<template>
	<!--pages/advanceSale/advanceSale.wxml-->
	<view class="all">
		<swiper v-if="banner" class="swiper" :autoplay="autoplay" :interval="interval" :duration="duration"
			circular="true">
			<block v-for="(item, index) in banner" :key="index">
				<swiper-item>
					<image mode="aspectFit" :src="item.image"></image>
				</swiper-item>
			</block>
		</swiper>
		<view class="advance-title">预售商品</view>
		<view class="goods-all">
			<view class="goods-list" v-for="(item, index) in goodsList" :key="index">
				<view class="goods-img">
					<image mode="aspectFit" :src="item.img"></image>
				</view>
				<view class="goods-information">
					<view class="goods-name">{{item.goods_name}}</view>
					<view class="goods-prompt">
						<view class="goods-text">
							<view class="advance-number">{{item.reserve_num}}人预定</view>
							<view class="advance-money" v-if="item.advance_type == 2">
								<view class="earnest-money">定金 ￥{{item.earnest_price}}</view>
								<view class="tail-money">尾款 ￥{{item.tail_price}}</view>
							</view>
							<view class="advance-money" v-if="item.advance_type == 1">
								<view class="earnest-money">预售价 ￥{{item.advance_price}}</view>
							</view>
						</view>
						<view class="goods-button">
							<view class="advance-button" @tap="reserve" :data-id="item.goods_id">预定</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 暂无数据 -->
			<view class="nodata" v-if="empty">暂无数据</view>
		</view>
	</view>
</template>

<script>
	// pages/advanceSale/advanceSale.js
	var app = getApp().globalData;

	export default {
		data() {
			return {
				autoplay: true,
				interval: 2000,
				duration: 1000,
				banner: "",
				//封面图
				goodsList: [],
				//预售商品列表
				page: 0, //页码
				empty: false,
				loadMore: true, //加载更多
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			if (options && (options.store_id)) {
				this.$store.store_id = options.store_id;
			}
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		async onShow() {
			await this.$onLaunched;
			this.getAllAdvanceList();
			this.getAdvanceBanner();
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {
			this.getAllAdvanceList();
		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {},
		methods: {
			//预售详情
			reserve: function(e) {
				var id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: '../advanceDetails/advanceDetails?id=' + id
				});
			},

			// 获取预售列表
			getAllAdvanceList() {
				var that = this;
				var page = parseInt(that.page) + 1;
				if (that.loadMore) {
					uni.showLoading({
						title: '加载中',
						// #ifndef MP-ALIPAY
						mask: true,
						// #endif
					});
					getApp().globalData.sendRequest({
						url: '/Goods/advanceList',
						data: {
							page: page,
							limit: 10
						},
						method: 'post',
						success(res) {
							uni.hideLoading();
							if (res.status == 1) {
								if (page == 1) {
									if (res.advanceList.length > 0) {
										that.setData({
											goodsList: res.advanceList,
											empty: false,
											loadMore: true,
											page: page
										});
									} else {
										that.setData({
											goodsList: res.advanceList,
											empty: true,
											loadMore: false
										});
									}
								} else {
									//非第一页
									let list_old = that.goodsList;
									let goodsList = [...list_old, ...res.advanceList];
									if (res.advanceList.length > 0) {
										that.setData({
											goodsList: goodsList,
											empty: false,
											loadMore: true,
											page: page
										});
									} else {
										uni.showToast({
											title: "已显示全部",
											icon: 'none',
											duration: 2000
										})
										that.setData({
											goodsList: goodsList,
											empty: false,
											loadMore: false
										});
									}
								}
							} else {
								//接口返回失败,页面有数据加载更多false,无数据显示暂无数据
								if (that.goodsList.length > 0) {
									that.setData({
										empty: false
									});
								} else {
									that.setData({
										empty: true
									});
								}
								that.setData({
									loadMore: false
								});
							}
						}
					});
				} else {
					uni.showToast({
						title: "已显示全部",
						icon: 'none',
						duration: 2000
					})
				}

			},

			//获取预售banner图片
			getAdvanceBanner() {
				var that = this;
				uni.showLoading({
					title: '加载中',
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				});
				getApp().globalData.sendRequest({
					url: '/Goods/advanceBanner',
					data: {},
					method: 'post',

					success(res) {
						uni.hideLoading();

						if (res.status == 1) {
							that.setData({
								banner: res.image
							});
						} else {}
					}

				});
			}

		}
	};
</script>
<style>
	@import "./advanceSale.css";

	.nodata {
		width: 100%;
		font-size: 30rpx;
		color: #999999;
		text-align: center;
		margin-top: 50rpx;
	}
</style>
